<?php
include("VerifySession.php");
//require_once('calendar/classes/tc_calendar.php');
?>
 
<?php
include("Header.php");
include("SNManager.class.php");
include("Pagination.php");
$Pagination = new Pagination();
$db = new SNDataManager();
$date_search = date('d').'/'. date('m').'/'. date('Y');
if(isset($_REQUEST['command']) && $_REQUEST['command']=='clear' && !isset($_REQUEST['page']) )
{
    unset($_SESSION["date_search_old"]);    
}
if(isset($_REQUEST['txtDateSearch']))
{
    $date_search = $_REQUEST['txtDateSearch'];
    $_SESSION["date_search_old"] = $date_search;        
}
else if(isset($_SESSION["date_search_old"]))
{    
    $date_search  = $_SESSION["date_search_old"];
}
//echo $_SESSION["date_search_old"];
//if(!isset($_REQUEST['page']))
//{    
    //unset( $_SESSION["date_search_old"]);
//}

$limit = $config['perpage'];

$totalrows = mysql_fetch_array($db->GetSNRowCount( $date_search));    

if(isset($_REQUEST['page']) && is_numeric(trim($_REQUEST['page']))){$page = mysql_real_escape_string($_REQUEST['page']);}else{$page = 1;}
    $startrow = $Pagination->getStartRow($page,$limit);

    //create page links
    if($config['showpagenumbers'] == true)
    {
    	$pagination_links = $Pagination->showPageNumbers($totalrows['total'],$page,$limit);
    }else
    {
        $pagination_links=null;
    }
    
    if($config['showprevnext'] == true)
    {
    	$prev_link = $Pagination->showPrev($totalrows['total'],$page,$limit);
    	$next_link = $Pagination->showNext($totalrows['total'],$page,$limit);
    }else
    {
        $prev_link=null;$next_link=null;
    }
    

    if(!isset($_REQUEST['orderby']) OR trim($_REQUEST['orderby']) == "")
    {
        $result =$db->GetTopSN(1);
        if(mysql_num_rows($result) >0)
        {
    	$array = mysql_fetch_assoc($result);
    	//first field
            	$i = 0;        
            	foreach($array as $key=>$value)
                {
            		if($i > 0){break;}else
                    {
            		  $orderby=$key;
                    }
            		$i++;		
            	}
        }
        $sort="ASC";    
    }else
    {
      $orderby = mysql_real_escape_string($_REQUEST['orderby']);
    }
    
    //IF SORT NOT SET OR VALID, SET DEFAULT
    if(!isset($_REQUEST['sort']) OR ($_REQUEST['sort'] != "ASC" AND $_REQUEST['sort'] != "DESC"))
    {
    	//default sort
    		$sort="ASC";
    	}else
        {	
    		$sort= mysql_real_escape_string($_REQUEST['sort']);
    }
    if(!$orderby) $orderby="SERIAL_NUMBER";
    
  
?>    
<div id="adbox">
<h2><span>Import Serial Number</span></h2>
 <form enctype="multipart/form-data" method="post" role="form" action="ImportCSV.php">
                <table width="750" border="0" align="center">
                  <tr>
                        <th width="100" align="right">File Upload</th>
                        <th width="300"><input name="file" type="file" id="file" size="50" width="200" /></th>
                        <th width="200" align="left"><span class="help-block">Only CSV or Text File Import. </span></th>
                    </tr>
                    <tr>
                        <th>&nbsp;</th>
                        <th><button type="submit" class="btnExample" name="Import" value="Import">Import File</button></th>
                        <th>&nbsp;</th>
                   </tr>
                </table>
                </form>
            </div> <!-- /article -->
            
 <div id="adbox">
 <h2><span>List Serial Number</span></h2>

<form id="from_search" name="from_search" method="POST" action=""> 

<div class="CSSTable" align="center">
<table width="450" border="0" align="center">
      <tr>     
        <th align="right" width="150"><label>GENERATE DATE</label></th>
        <th align="left"  width="250">        
         <input type="text" id="txtDateSearch" name="txtDateSearch" 
                data-beatpicker="true" 
                data-beatpicker-setDate="<?php echo $date_search; ?>"
                data-beatpicker-module="gotoDate" 
                data-beatpicker-format="['YYYY','MM','DD'],separator:'/'"
                data-beatpicker-id="myDatePicker" />
        </th>
        <th align="left" width="50"><input id="btSearch" type="submit" value="Search" /></th>
      </tr>
      </table>
</div>
<div class="CSSTable" align="center">
<table width="1024" border="0" align="center">
      <tr>
      <?php
       $result_allsn = $db->getAllSNPaging($orderby,$sort, $startrow,$limit,$date_search); 
       $row_count= mysql_num_rows($result_allsn);
        if($row_count >0)
      {
          $array = mysql_fetch_assoc($result_allsn);
            foreach ($array as $key=>$value) 
            {     
                if($key!='ID')
                {
            	if($config['nicefields'])
                {
                	$field = str_replace("_"," ",$key);
                	$field = ucwords($field);
            	}
            	
            	$field = $db->columnSortArrows($key,$field,$orderby,$sort);
            	echo "<th>" . $field . "</th>\n";
                }
            }
            echo "<th>&nbsp;</th>\n";
            echo "</tr>\n";
            mysql_data_seek($result_allsn,0);       
         while ($row = mysql_fetch_array($result_allsn, MYSQL_ASSOC))
         { ?>
       <tr>       
        <td><?php echo $row['SERIAL_NUMBER']; ?></td>
        <td><?php echo $row['GENERATE_DATE']; ?></td>
        <td><?php echo $row['IMPORT_DATE']; ?></td>
        <td><?php echo $row['IMPORT_BY']; ?></td>
        <td><a href="DelSNNumber.php?UID=<?php echo $row['ID']; ?>" onclick="return confirm('Are you sure you want to delete this?')" >DELETE</a></td>
      </tr>
      <?php 
       }
        }else
        { ?>
            <td>SERIAL NUMBER</td>
            <td>GENERATE DATE</td>
            <td>IMPORT DATE</td>
            <td>IMPORT BY</td>
            <td>&nbsp;&nbsp;&nbsp;</td>
      <?php  } ?>
       <tr>
    <th colspan="5" align="center" >
                <?php
            if(!($prev_link==null && $next_link==null && $pagination_links==null)){
            echo '<div class="pagination">';
            echo $prev_link;
            echo $pagination_links;
            echo $next_link;
            echo '<div style="clear:both;"></div>';
            echo "</div>";
            }
            ?>
    </th>
    </tr>
    </table>
     </form>
    </div>
                </p>
            </div> <!-- /article -->

<script type="text/javascript">
            $(document).ready(function(){
                var statusGenerator = function (text) {
                    var statusElem = $(".status-box");
                    var child = $("<span style='display: block'></span>").text(text);
                    statusElem.append(child);
                };
                myDatePicker.on("select", function (data) {
                    statusGenerator(data.string + " selected")
                });
                myDatePicker.on("change", function (data) {
                    statusGenerator("Date picker changed current date: "+data.string);
                });
                myDatePicker.on("show", function () {
                    statusGenerator("Date picker show")
                });
                myDatePicker.on("clear", function (data) 
                {   
                    //document.getElementById('txtDateSearch').value = "";
                    statusGenerator("Date picker cleared. cleared date: " + data.string)
                });
                myDatePicker.on("hide", function () {
                    statusGenerator("Date picker hide")
                });
            })

        </script>   
        
  <?php
  include("Footer.php");
  ?>
